import Axios from 'axios'
import Cookies from 'js-cookie';
import router from '../router';

Axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
Axios.defaults.headers.put['Content-Type'] = 'application/json; charset=utf-8';

// Axios.defaults.baseURL = 'http://127.0.0.1:8080'; //dev
// Axios.defaults.baseURL = 'http://kid-debug.color-sz.com/debug';//debug
Axios.defaults.baseURL = 'http://wy.wekids.group/pro';//pro




const baseURL = 'http://wy.wekids.group:8089/api'; //dev

export const AxiosNew = Axios.create({
    baseURL: baseURL,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
});
const post1 = (url, params = {}) => {
    return AxiosNew.post(url, params, {}).then(response => {
        if (response.data && response.data.code === 200) {
            return response.data;
        }
        throw response;
    }).catch(error => {
        return Promise.reject(error.data || error);
    });
};
const fetch1 = (url, params = {}) => {
    return AxiosNew.get(url, {
        params: params,
    }).then(response => {
        return response.data;
    }).catch(error => {
        return Promise.reject(error.data || error);
    });
};


/**
 * 拦截器
 */
Axios.interceptors.request.use(
    config => {
        let token = Cookies.get('token') !== 'undefined' ? Cookies.get('token') : "";
        config.headers = {
            Authorization: "Bearer " + token,
        };
        return config;
    }
);
Axios.interceptors.response.use(
    response => {
        if (response.data.code === 200) {
            return response;
        } else if (response.data.code === 1000) {
            router.push({
                path: '/index',
                query: {
                    redirect: router.currentRoute.fullPath
                }
            });
        } else {
            alert(response.data.msg);
        }
    },
)

const fetch = (url, params = {}) => {
    return Axios.get(url, {
        params: params,
    }).then(response => {
        return response.data;
    }).catch(error => {
        return Promise.reject(error.data || error);
    });
};

const post = (url, params = {}) => {
    return Axios.post(url, params, {}).then(response => {
        if (response.data && response.data.code === 200) {
            return response.data;
        }
        throw response;
    }).catch(error => {
        return Promise.reject(error.data || error);
    });
};

const put = (url, params = {}) => {
    return Axios.put(url, params, {}).then(response => {
        if (response.data && response.data.code === 200) {
            return response.data;
        }
        throw response;
    }).catch(error => {
        return Promise.reject(error.data || error);
    });
};

const del = (url, params = {}) => {
    return Axios.delete(url, {
        params: params,
    }).then(response => {
        if (response.data && response.data.code === 200) {
            return response.data;
        }
    }).catch(error => {
        return Promise.reject(error.data || error);
    });
};

export default {
    del,
    fetch,
    //微信配置
    fetchWxConfig: fetch.bind(null, '/wx/common/config'),
    checkUserBind: fetch.bind(null, '/wx/common/check'),
    bindGardener: post.bind(null, '/wx/common/bind'),
    changeKindergarten: fetch.bind(null, 'wx/common/kindergarten/change'),
    fetchRedirectUrl: (selectType) => fetch.bind(null)('/wx/common/redirect-url/' + selectType),

    //查询学生信息
    fetchStuInfo: fetch.bind(null, 'wx/common/stu/info'),
    //绑定学生
    bindStu: post.bind(null, 'wx/bind/stu'),
    bindStuV2: post.bind(null, 'wx/bind/v2/stu'),
    //绑定查询
    bindState: fetch.bind(null, '/wx/bind/stu/status'),
    //解除绑定
    unbindStu: fetch.bind(null, '/wx/student/unbind'),
    fetchStudentList: fetch.bind(null, '/wx/student'),
    fetchKindergartenStu: fetch.bind(null, '/wx/student/query'),
    fetchKindergartenStuV2: fetch.bind(null, '/wx/student/v2/query'),
    //教师解绑
    unbindTea: post.bind(null, '/wx/teacher/unbind'),

    //简介
    fetchKindergarten: fetch.bind(null, '/wx/kindergarten'),

    fetchKindergartenList: fetch.bind(null, '/wx/kindergarten/list'),

    fetchKindergartenShareInfoById: fetch.bind(null, '/wx/share/kindergarten/info'),

    //安全围栏绑定
    securityfenceBind: post.bind(null, '/wx/safetyfence/submit'),
    //安全围栏绑定-家庭
    securityfenceHomeBind: post.bind(null, '/wx/safetyfence/uphome'),
    //安全围栏绑定-学校
    securityfenceSchoolBind: post.bind(null, '/wx/safetyfence/upschool'),
    //安全围栏绑定-我的位置需要参数
    securityfenceaddressBind: fetch.bind(null, '/wx/safetyfence/address'),
    //请求
    securityfenceNum: fetch.bind(null, '/wx/safetyfence/query'),

    //教师请假审核
    putTeaLeaveJudge: put.bind(null, '/wx/tea-leave/judge'),
    //学校简介

    updateKindergarten: (data) => put.bind(null)('/wx/kindergarten/info', data),
    //教师请假审核
    fetchTeacherLeave: fetch.bind(null, 'wx/teacher-leave'),
    judgeTeacherLeave: (data) => put.bind(null)('/wx/teacher-leave/judge', data),
    //教师个人请假记录
    fetchTeaLeavePers: fetch.bind(null, '/wx/tea-leave'),
    //教师提交请假
    postTeaLeavePers: post.bind(null, '/wx/tea-leave'),

    //学校简介

    //幼儿管理

    fetchOutApply: fetch.bind(null, '/wx/out-apply'), //离园确认
    //个人中心学生信息
    fetchMyStudent: fetch.bind(null, '/wx/student/info'),

    fetchPick: fetch.bind(null, '/wx/pick'), //接送记录
    //体检报告
    fetchKinsfolkReport: fetch.bind(null, '/wx/report'),
    fetchReportDetail: (id) => fetch.bind(null)('/wx/report/' + id),
    //家长反馈
    fetchFeedback: fetch.bind(null, 'wx/feedback'),
    createFeedback: post.bind(null, 'wx/feedback'),
    readKinsfolkFeedback: (data) => put.bind(null)('/wx/feedback/read', data),
    //离园申请
    fetchOutLeaveApply: fetch.bind(null, '/wx/out-apply'),
    createOutApply: post.bind(null, '/wx/out-apply'),
    fetchOutLeaveApplyById: (id) => fetch.bind(null)('/wx/out-apply/' + id),
    //学生请假
    fetchStuLeave: fetch.bind(null, '/wx/student/leave'), //请假记录
    createStuLeave: post.bind(null, '/wx/student/leave'),
    //家长首页统计
    fetchKinsfolkStudentStatics: fetch.bind(null, '/wx/student/kinsfolk/statics'),
    //管理员首页头统计
    fetchAdminStatics: fetch.bind(null, '/wx/common/statics'),
    //教师首页统计
    fetchTeacherStatics: fetch.bind(null, '/wx/student/teacher/statics'),
    //通知公告
    fetchNotice: fetch.bind(null, '/wx/notice'),
    deleteNotice: del.bind(null, '/wx/notice'),
    //班级
    fetchClazzList: fetch.bind(null, '/wx/clazz/list'), //获取班级列表
    //上传
    uploadFile: post.bind(null, '/cos/upload'),
    uploadList: post.bind(null, 'cos/upload/list'),
    deleteFile: del.bind(null, '/cos/del'),
    getCosCredential: fetch.bind(null, '/cos/credential'),
    //班级动态
    createTrendy: post.bind(null, '/wx/trends'),
    fetchTrendy: fetch.bind(null, '/wx/trends'),
    deleteTrendy: del.bind(null, '/wx/trends'),
    fetchShareTrendsById: fetch.bind(null, 'wx/share/trends/info'),
    converTrendy: (data) => put.bind(null)('wx/trends/conver', data),
    handlePraiseFn: (data) => put.bind(null)('wx/trends/praise', data),
    //公告
    postNotice: post.bind(null, '/wx/notice'),
    fetchHomeNoticeRemind: fetch.bind(null, '/wx/notice/home/remind'),
    kinsfolkReadNotice: post.bind(null, '/wx/notice/read/kinsfolk'),
    kinsfolkConfirmNotice: post.bind(null, '/wx/notice/confirm/kinsfolk'),
    fetchNoticeKinsfolk: fetch.bind(null, '/wx/notice/kinsfolk'),
    readNotice: post.bind(null, '/wx/notice/read'),
    //家长列表
    fetchReceiver: fetch.bind(null, '/wx/notice/receiver'),
    //晨检报告列表
    fetchReport: fetch.bind(null, '/wx/report'),
    //通讯录
    fetchAddressBook: fetch.bind(null, '/wx/common/address-book'),

    //新增相册
    postAlbumMain: post.bind(null, '/wx/album/main'),
    //删除相册
    deleAlbumMain: del.bind(null, '/wx/album/main'),
    //删除照片
    deleAlbumImages: del.bind(null, '/wx/album/image'),
    //查看相册
    fetchAlbumMain: fetch.bind(null, '/wx/album/main'),
    //查看相册图片
    fetchAlbumImageList: fetch.bind(null, '/wx/album/image/list'),
    //新增照片
    postAlbum: post.bind(null, '/wx/album'),
    fetchAtd: fetch.bind(null, '/wx/atd'),
    fetchAtdItem: fetch.bind(null, '/wx/atd/item'),
    loginByOpenId: fetch.bind(null, '/wx/common/login/openId'),
    getMsgDetail: fetch.bind(null, '/wx/common/msg/detail'),
    getKinsfolkInfo: fetch.bind(null, '/wx/bind/openId'),
    bindSecondChild: post.bind(null, '/wx/bind/second'),
    uploadFaceImage: post.bind(null, '/wx/common/face/image'),
    fetchFaceStatus: fetch.bind(null, '/wx/common/face/status'),
    //设备
    fetchDeviceList: fetch.bind(null, '/wx/device/list'),
    fetchDeviceById: (id) => fetch.bind(null)('/wx/device/' + id),
    updateDevice: post.bind(null, '/wx/device'),
    //人脸审核
    judgeFace: data => put.bind(null)('/wx/face-record/judge', data),
    fetchFaceRecord: fetch.bind(null, '/wx/face-record'),
    fetchKinsfolkJudge: fetch.bind(null, '/wx/kinsfolk-judge'),
    killJudge: (data) => put.bind(null)('/kinsfolk-judge/kill', data),
    passJudge: data => put.bind(null)('/kinsfolk-judge/pass', data),

    fetchCamera: fetch.bind(null, '/wx/camera'),
    fetchKinsfolkCamera: fetch.bind(null, '/wx/camera/kinsfolk/list'),
    loginInKindergarten: post.bind(null, '/wx/kinsfolk/login'),
    //注册绑定

    fetchRegisterCode: (code) => fetch.bind(null)('/wx/common/v2/redirect-url/' + code),

    //支付
    getWxConfig: fetch.bind(null, '/wx/common/web/config'),
    getPerPay: fetch.bind(null, '/wx/common/pre-pay'),
    getProduct: fetch.bind(null, '/wx/product'),
    getPayStatus: fetch.bind(null, '/wx/common/pay/status'),

    fetchStudentRecord: fetch.bind(null, '/wx/student-record'),

    fetchStudentRecordForKinsfolk: fetch.bind(null, '/wx/student-record/kinsfolk'),


    fetchKinsfolkStudentList: fetch.bind(null, '/wx/student/list'),
    freshStudentLocation: fetch.bind(null, '/wx/student/gps/fresh'),
    bindGps: post.bind(null, '/wx/student/gps/bind'),
    unbindGps: post.bind(null, '/wx/student/gps/unbind'),
    fetchGpsStatus: fetch.bind(null, '/wx/student/gps/status'),
    fetchGpsLists: post.bind(null, '/wx/student/gps/lists'),
    fetchGpsSosNum: post.bind(null, '/wx/student/gps/sosnum'),
    fetchGpsSosCon: post.bind(null, '/wx/student/gps/soscon'),
    upSosCon: post.bind(null, '/wx/student/gps/sosupcon'),
    fetchKinsfolkStatus: fetch.bind(null, '/wx/kinsfolk/status'),
    fetchKindergartenInfo: fetch.bind(null, '/wx/kindergarten/info'),
    getKindergartenNameAndLogo: fetch.bind(null, '/wx/kindergarten/name/logo'),


    getKinsfolkList: fetch.bind(null, '/wx/kinsfolk/student'),
    fetchKinsfolkStudentTodayReport: fetch.bind(null, '/wx/report/kinsfolk/student/today'),
    getGradeList: fetch.bind(null, '/wx/grade/list'),

    getClazzListByGradeId: fetch.bind(null, '/wx/clazz/selectByGradeId'),
    fetAllClazzList: fetch.bind(null, '/wx/clazz/all'),
    fetchStudentRecordStatics: fetch.bind(null, '/wx/student-record/statics'),
    fetchStudentReportStaticsForTeacher: fetch.bind(null, '/wx/report/statics/kindergarten'),
    fetchTeacherStuReportStatics: fetch.bind(null, '/wx/report/statics/teacher'),
    fetchStudentReportStaticsForKinsfolk: fetch.bind(null, '/wx/report/statics/kinsfolk'),
    getKinsfolkHomeStatics: fetch.bind(null, '/wx/student-record/home/kinsfolk/statics'),
    fetchHomeMenu: fetch.bind(null, '/wx/home/menu'),
    fetchAdminStudentStatics: fetch.bind(null, '/wx/student/kindergarten/statics'),
    fetchAdminTeacherStatics: fetch.bind(null, '/wx/teacher/kindergarten/statics'),
    updateHomeMenu: post.bind(null, '/wx/home/menu'),
    fetchArea: (id) => fetch.bind(null)('/wx/area/' + id),
    registerBind: post.bind(null, '/wx/kinsfolk/bind'),


    //食谱
    fetchCookbook: fetch.bind(null, '/wx/cookbook/week'),
    createCookbook: post.bind(null, '/wx/cookbook'),
    deleteCookbook: del.bind(null, '/wx/cookbook'),
    fetchShareCookbookInfo: fetch.bind(null, '/wx/share/cookbook/info'),

    fetchFoodShareInfo: fetch.bind(null, '/wx/share/food/info'),
    fetchFood: fetch.bind(null, '/wx/food'),
    createFood: post.bind(null, '/wx/food'),
    deleteFood: del.bind(null, '/wx/food'),

    //个人散户
    fetchPersonalList: fetch.bind(null, '/wx/personal/list'),
    fetchPersonalbinding: post.bind(null, '/wx/personal/binding'),
    fetchPersonalnoe: fetch.bind(null, '/wx/personal/noe'),
    fetchPersonaupdatelnoe: post.bind(null, '/wx/personal/updatePers'),
    fetchPersonalGps: fetch.bind(null, '/wx/personal/gpscode'),
    fetchPersonalBind: post.bind(null, '/wx/personal/bind'),


    //新接口，地址不同
    getTest1: fetch1.bind(null, '/test'),
    getKinsfolkList1: (kinsfolkId, loginType) => fetch1.bind(null)('/getLiveUrl/' + kinsfolkId + '/' + loginType),
    getDecrypt1: (encrypt, salt) => fetch1.bind(null)('/getDecrypt/' + encrypt + '/' + salt),
}
